package com.zd02.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zd02.entity.BookBook;
import com.zd02.mapper.BookBookMapper;
import com.zd02.service.IBookBookService;
import com.zd02.service.impl.BookBookServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangdan
 * @since 2025-06-22
 */
@RestController
@RequestMapping("/book-book")
@RequiredArgsConstructor
public class BookBookController {
    // service 层面的玩法
    private final BookBookServiceImpl bookServiceImpl;
    @GetMapping("/serviceImpl")
    public List<BookBook> selectALl(){
        return bookServiceImpl.list();
    }
    @GetMapping("/serviceImpl/page")
    public List<BookBook> selectALlPage(){
        return bookServiceImpl.selectALlPage();
    }

    @GetMapping("/serviceImpl/condition")
    public List<BookBook> selectCondition(){
        return bookServiceImpl.lambdaQuery()
                .gt(BookBook::getId, 0)
                .like(BookBook::getBook_name, "java")
                .list();
    }
    @GetMapping("/serviceImpl/condition2")
    public List<BookBook> selectCondition2(){
        QueryWrapper<BookBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", 0);
        System.out.println("queryWrapper".repeat(20));
        System.out.println(queryWrapper);
        return bookServiceImpl.list(queryWrapper);
    }

    @PostMapping("/serviceImpl")
    public void serviceImplSave(@RequestBody BookBook bookBook){
        bookBook.setAdd_time(LocalDate.now());
        bookServiceImpl.save(bookBook);
    }

    @PostMapping("/serviceImpl/saveUpdate")
    public ResponseEntity<?> serviceImplSaveUpdate(@RequestBody BookBook bookBook){
        boolean result = bookServiceImpl.saveOrUpdate(bookBook);
        return result ?
                ResponseEntity.ok("操作成功") :
                ResponseEntity.status(500).body("操作失败");
    }
    @DeleteMapping("/serviceImpl/deleteCondition")
    public void deleteCondition(){
        QueryWrapper<BookBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_name", "java");
        bookServiceImpl.remove(queryWrapper);
    }
    @DeleteMapping("/serviceImpl/deleteByid")
    public void deleteByid(){
        bookServiceImpl.removeById(17);
    }
    @DeleteMapping("/serviceImpl/deleteByids")
    public void deleteByids(){
        List<Integer> ids = Arrays.asList(1, 2, 3);
        bookServiceImpl.removeByIds(ids);
    }

    @PutMapping("/serviceImpl/updateById")
    public void updateById(@RequestBody BookBook bookBook){
        bookServiceImpl.updateById(bookBook);
    }

    @PutMapping("/serviceImpl/condition")
    public void condition(){
        bookServiceImpl.lambdaUpdate()
                .like(BookBook::getBook_name, "java")
                .set(BookBook::getStatus, 0)
                .set(BookBook::getBook_name,"数码宝贝")
                .update();

    }

    @PutMapping("/serviceImpl/condition2")
    public void condition2(){
        UpdateWrapper<BookBook> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",16).set("book_name", "西游记");
        bookServiceImpl.update(updateWrapper);
    }




    // mapper 层面的玩法
    private final BookBookMapper bookMapper;
    @PostMapping("/mapper")
    public void mapperSave() {
        BookBook b = new BookBook();
        b.setAdd_time(LocalDate.now());
        b.setBook_name("java2");
        b.setStatus(1);
        bookMapper.insert(b);
    }
    @PostMapping("/mapper/serviceImpl")
    public void mapperServiceImpl() {
        bookServiceImpl.mapperServiceImpl();
    }

    // intertface 层面的玩法
    private final IBookBookService iBookBookService;
    @PostMapping("/intertface")
    public void add2() {

        BookBook b= new BookBook();
        b.setBook_name("java");
        b.setStatus(0);
        b.setAdd_time(LocalDate.now());
        iBookBookService.save(b);

    }
    @PostMapping("/intertface/serviceImpl")
    public void serviceImpl() {
        bookServiceImpl.serviceImpl();
    }
}
